package com.ngc.mall.common.configure;

import com.ngc.mall.domain.UserDomain;
import com.ngc.mall.domain.WxUserDomain;

import java.util.HashMap;
import java.util.Map;

public class LocalUser {
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    public static void set(WxUserDomain user) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        LocalUser.threadLocal.set(map);
    }

    public static void clear() {
        LocalUser.threadLocal.remove();
    }

    public static WxUserDomain getUser() {
        Map<String, Object> map = LocalUser.threadLocal.get();
        WxUserDomain user = (WxUserDomain)map.get("user");
        return user;
    }

    public static Integer getScope() {
        Map<String, Object> map = LocalUser.threadLocal.get();
        Integer scope = (Integer)map.get("scope");
        return scope;
    }

    public static void setLocalUser(UserDomain user){
        Map<String,Object> map=new HashMap<>();
        map.put("user",user);
        LocalUser.threadLocal.set(map);
    }
    public static UserDomain getLocalUser(){
        Map<String,Object> map= LocalUser.threadLocal.get();
        UserDomain domain=(UserDomain) map.get("user");
        return domain;
    }

    /**
     * 清理当前用户
     */
    public static void clearLocalUser() {
        LocalUser.threadLocal.remove();
    }

}